package Datastruct.Clue;

public class ClueBinaryTree {

    private HeroNode root;

    //当前结点的前驱结点
    private HeroNode pre = null;

    public void setRoot(HeroNode root) {
        this.root = root;
    }


    //调用这个方法就相当于把非线索化的二叉树转化为线索化的二叉树
    public void cluNode() {
        this.cluNode(root);
    }


    /**
     * 把普通二叉树转为线索化二叉树
     * <p>
     * root
     */
    public void cluNode(HeroNode node) {
        //判断是否可以线索化当前结点
        if (node == null) {
            return;
        }

        //先线索化左子树
        cluNode(node.getLeft());

        //处理当前结点的前驱
        if (node.getLeft() == null) {
            node.setLeft(pre);
            node.setNoleft(1);
        }
        //处理当前结点的后继
        if (pre != null && pre.getRiight() == null) {
            pre.setRiight(node);
            pre.setNoright(1);
        }
        //保证处理完一个结点后，就要让当前结点作为下一个结点的前驱
        pre = node;

        //线索化右子树
        cluNode(node.getRiight());


    }

    //遍历线索化二叉树
    public void clueLIst() {
        //临时结点
        HeroNode node = root;
        while (node != null) {

            //向左查找头结点
            while (node.getNoleft() == 0) {
                node = node.getLeft();

            }
            System.out.println(node);

            while (node.getNoright() == 1) {
                node = node.getRiight();
                System.out.println(node);
            }

            node = node.getRiight();
        }
    }
}
